import dateUtil from 'element-ui/src/utils/date'

export const toDate = (date) => {
  return isDate(date) ? new Date(date) : null
}

export const isDate = (date) => {
  if (date === null || date === undefined) return false
  if (isNaN(new Date(date).getTime())) return false
  return true
}

export const isDateObject = (val) => {
  return val instanceof Date
}

export const formatDate = (date, format) => {
  date = toDate(date)
  if (!date) return ''
  return dateUtil.format(date, format || 'yyyy-MM-dd HH:mm:ss')
}

export const parseDate = (string, format) => {
  return dateUtil.parse(string, format || 'yyyy-MM-dd HH:mm:ss')
}
